package com.wzb.mybatisplus.bitaction.electrumaction.five;

import com.wzb.mybatisplus.bitaction.electrumaction.FileUtils;
import org.bitcoinj.core.*;
import org.bitcoinj.params.MainNetParams;
import java.math.BigInteger;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

public class ParallelSequentialFinder {

    private static final NetworkParameters params = MainNetParams.get();
    private static final AtomicInteger foundCount = new AtomicInteger(0);
    private static final AtomicReference<BigInteger> current = new AtomicReference<>(BigInteger.ONE);
    private static String path = "D:\\btc\\miyao\\maney5.txt";
    private static FileUtils fileUtils = new FileUtils();

    public static void main(String[] args) throws InterruptedException {
//        System.out.println("多线程顺序查找以5开头的私钥");
//
//        int threadCount = 4; // 线程数
//        int targetCount = 10; // 目标找到的数量
//
//        Thread[] threads = new Thread[threadCount];
//
//        for (int i = 0; i < threadCount; i++) {
//            threads[i] = new Thread(new KeyFinderWorker(i, targetCount));
//            threads[i].start();
//        }
//
//        // 等待所有线程完成或达到目标
//        for (Thread thread : threads) {
//            thread.join();
//        }
//
//        System.out.println("\n查找完成! 总共找到 " + foundCount.get() + " 个私钥");








    }

    static class KeyFinderWorker implements Runnable {
        private final int workerId;
        private final int targetCount;

        public KeyFinderWorker(int workerId, int targetCount) {
            this.workerId = workerId;
            this.targetCount = targetCount;
        }

        @Override
        public void run() {
//            System.out.println("Worker " + workerId + " 启动");

            while (foundCount.get() < targetCount) {
                BigInteger myCurrent = current.getAndUpdate(bi -> bi.add(BigInteger.ONE));

                if (myCurrent.compareTo(new BigInteger("1000000000")) > 0) {
                    break; // 限制查找范围，避免无限循环
                }

                try {
                    ECKey key = ECKey.fromPrivate(myCurrent);
                    String privateKeyWIF = key.getPrivateKeyAsWiF(params);

                    if (privateKeyWIF.startsWith("5")) {
                        int count = foundCount.incrementAndGet();
                        if (count <= targetCount) {
                            synchronized (System.out) {
                                System.out.println("\n[Worker " + workerId + "] 找到第 " + count + " 个私钥:");
                                System.out.println("数值: " + myCurrent);
                                System.out.println("WIF: " + privateKeyWIF);
                                fileUtils.writeFile("地址: " + LegacyAddress.fromKey(params, key)+"数值: " + myCurrent+"WIF: " + privateKeyWIF,path);
                                System.out.println("地址: " + LegacyAddress.fromKey(params, key));
                            }
                        }
                    }
                } catch (Exception e) {
                    // 继续下一个
                }

                // 进度显示
//                if (myCurrent.mod(BigInteger.valueOf(100000)).equals(BigInteger.ZERO)) {
//                    System.out.printf("Worker %d: 已检查 %s, 找到 %d/%d\n",
//                            workerId, myCurrent, foundCount.get(), targetCount);
//                }
            }

//            System.out.println("Worker " + workerId + " 结束");
        }
    }
}
